/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GameResultInputPanel.java
 *
 * Created on 2009-9-27, 16:38:54
 */
package com.res0w.SMMS.ui.admin;

import com.res0w.SMMS.factory.AutoCompleter;
import com.res0w.SMMS.factory.DataBaseOperate;
import com.res0w.SMMS.factory.JAutoCompleteComboBox;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author res0w
 * @version 0.1
 * @since 2009-10-6
 * 
 */
public class GameResultInputPanel extends javax.swing.JPanel {

    /** Creates new form GameResultInputPanel */
    public GameResultInputPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        results = new javax.swing.JTextField();
        addAlterBtn = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        itemsCombox = new JAutoCompleteComboBox(items);
        atheleteIDText = new javax.swing.JTextField();

        setBackground(new java.awt.Color(255, 255, 255));
        setFont(new java.awt.Font("微软雅黑", 0, 12));
        setPreferredSize(new java.awt.Dimension(950, 530));

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 18));
        jLabel1.setText("成绩录入：");

        listTable.setFont(new java.awt.Font("微软雅黑", 0, 12)); // NOI18N
        listTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},
                {"", "", "", "", ""},},
            new String[]{
                "运动员号", "学号", "姓名", "参加项目", "成绩"
            }) {

                boolean[] canEdit = new boolean[]{
                    false, false, false, false, false
                };

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
            });
            jScrollPane1.setViewportView(listTable);

            jLabel2.setFont(new java.awt.Font("微软雅黑", 0, 12));
            jLabel2.setText("运动员号：");

            jLabel3.setFont(new java.awt.Font("微软雅黑", 0, 12));
            jLabel3.setText("成绩：");

            results.setFont(new java.awt.Font("微软雅黑", 0, 12));

            addAlterBtn.setFont(new java.awt.Font("微软雅黑", 0, 12));
            addAlterBtn.setText("添加/修改");
            addAlterBtn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    addAlterBtnActionPerformed(evt);
                }
            });

            jButton2.setFont(new java.awt.Font("微软雅黑", 0, 12));
            jButton2.setText("提交");

            jLabel4.setFont(new java.awt.Font("微软雅黑", 0, 14));
            jLabel4.setText("使用提示：");

            jTextArea1.setColumns(20);
            jTextArea1.setFont(new java.awt.Font("微软雅黑", 0, 12));
            jTextArea1.setRows(5);
            jTextArea1.setText("\n最上方的【运动员号】和【成绩】两栏\n用以输入运动员的学号和比赛成绩\n【项目一栏】选择运动员产生成绩的项目\n\n在左侧的表格中显示当前可录入的\n项目清单；\n通过【添加/修改】按钮可以修改表格中\n的成绩信息；\n通过【提交】按钮可以提交整个表格信息\n\n注意：\n一旦提交运动员成绩信息便不能修改\n需要更高权限的账户才可以进行错误更正\n操作（会记录操作日志）");
            jScrollPane2.setViewportView(jTextArea1);

            jLabel5.setFont(new java.awt.Font("微软雅黑", 0, 12));
            jLabel5.setText("项目：");

            itemsCombox.setFont(new java.awt.Font("微软雅黑", 0, 12));
            AutoCompleter.setItems(items);
            itemsCombox.addActionListener(itemsCombox);

            atheleteIDText.setFont(new java.awt.Font("微软雅黑", 0, 12));

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(67, 67, 67)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(1, 1, 1)
                                    .addComponent(atheleteIDText, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel5)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(itemsCombox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(32, 32, 32)
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(results, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(27, 27, 27)
                                    .addComponent(addAlterBtn)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(112, 112, 112)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(32, 32, 32)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(27, 27, 27)
                                    .addComponent(jLabel4)))))
                    .addContainerGap(26, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(45, 45, 45)
                            .addComponent(jLabel1)
                            .addGap(17, 17, 17)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(jLabel5)
                                .addComponent(itemsCombox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3)
                                .addComponent(results, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(addAlterBtn)
                                .addComponent(atheleteIDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(78, 78, 78)
                            .addComponent(jLabel4)
                            .addGap(18, 18, 18)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jButton2)))
                    .addContainerGap(12, Short.MAX_VALUE))
            );
        }// </editor-fold>//GEN-END:initComponents

    private void addAlterBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAlterBtnActionPerformed
        // TODO add your handling code here:
        addAlter();
    }//GEN-LAST:event_addAlterBtnActionPerformed

//Create By Author
    private Object[] itemsId = loadItems(1);
    private Object[] items = loadItems(0);

    private Object[] loadItems(int m) {
        String sql = null;
        if (m == 0) {
            sql = "select distinct itemsName from setting_schedule";
        } else if (m == 1) {
            sql = "select distinct athleteId from signup_cachelist";
        }
        ArrayList list = new ArrayList();
        try {
            int rowNum = 0;

            ResultSet rs = null;
            DataBaseOperate db = new DataBaseOperate();
            rs = db.dbQueryMuti(sql);
            while (rs.next()) {
                list.add(rs.getString(1));
                rowNum++;
            }
            db.dbClosed();
        } catch (SQLException ex) {
            Logger.getLogger(GameRollCallPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        Object[] re = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
            re[i] = list.get(i);
        }
        return re;
    }

    private void addAlter() {
        String atheleteID;
        String itemsName;
        String result;
        String sID = null;
        String sName = null;
        atheleteID = atheleteIDText.getText().toString().trim();
        itemsName = itemsCombox.getSelectedItem().toString().trim();
        result = results.getText().toString().trim();
        boolean isFind = false;
        //修改成绩
        for (int i = 0; i < listTable.getModel().getRowCount(); i++) {
            if (atheleteID.equals(listTable.getValueAt(i, 0).toString().trim()) &&
                    itemsName.equals(listTable.getValueAt(i, 3).toString().trim())) {
                listTable.setValueAt(result, i, 4);
                isFind = true;
                break;
            }
        }
        //添加成绩
        if (!isFind) {
            try {
                DataBaseOperate db = new DataBaseOperate();
                ResultSet rs = null;
                String sql = "select * from signup_cachelist " +
                        "where athleteId='" + atheleteID + "' and joinItems='" + itemsName + "'";
                rs = db.dbQueryMuti(sql);
                while (rs.next()) {
                    sID = rs.getString(4);
                    sName = rs.getString(5);
                }
                db.dbClosed();
            } catch (SQLException ex) {
                Logger.getLogger(GameResultInputPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (int p = 0; p < listTable.getModel().getRowCount(); p++) {
                if (listTable.getValueAt(p, 0).toString().trim() == "") {
                    listTable.setValueAt(atheleteID, p, 0);
                    listTable.setValueAt(sID, p, 1);
                    listTable.setValueAt(sName, p, 2);
                    listTable.setValueAt(itemsName, p, 3);
                    listTable.setValueAt(result, p, 4);
                    break;
                }
            }
        }
//        isFind = false;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAlterBtn;
    private javax.swing.JTextField atheleteIDText;
    private javax.swing.JComboBox itemsCombox;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTable listTable;
    private javax.swing.JTextField results;
    // End of variables declaration//GEN-END:variables
}
